<?php
/**
 * @title 137. 只出现一次的数字 II - 位运算
 * @author start2004
 */

// ini_set("memory_limit", "128M");

class Solution {

    /**
     * @param Integer[] $nums
     * @return Integer
     */
    function singleNumber($nums) {
        /**
         * @since 2020-08-21 位掩码
         */
        $seen_once = $seen_twice = 0;

        /**
         * @since 2020-08-21 遍历
         */
        foreach ($nums as $num){
            $seen_once = ~$seen_twice & ($seen_once ^ $num);
            $seen_twice = ~$seen_once & ($seen_twice ^ $num);
        }

        /**
         * @return
         */
        return $seen_once;
    }
}



/**
 * @url http://127.0.0.1/leetcode/202008/2020.08.07_5.php
 */
$datas = [
    // [2,2,3,2],
    // [0,1,0,1,0,1,99],
    [0,1,0,1,0,1,2],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->singleNumber($data);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
